package com.spider.silence.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 
 * PropertiesUtil.java
 *
 * @desc properties 资源文件解析工具
 * @author Guoxp
 * @datatime Apr 7, 2013 3:58:45 PM
 *
 */
public class PropertiesUtil {
    private Properties props;
    private static PropertiesUtil instance=new PropertiesUtil("META-INF/settings.properties");
    private static Properties logProperties;
    private PropertiesUtil(){}
    public static String getVal(String key){
        return instance.getProperty(key);
    };
    private PropertiesUtil(String fileName){
        readProperties(fileName);  
    }  
    private void readProperties(String fileName) {  
        try {  
            props = new Properties();
            InputStream fis =this.getClass().getResourceAsStream("/"+fileName);
            props.load(fis);  
        } catch (Exception e) {
            e.printStackTrace();  
        }  
    }

    private synchronized  Properties geLogProperties() throws IOException {
        if(logProperties==null){
            logProperties = new Properties();
            InputStream fis =this.getClass().getResourceAsStream("/META-INF/log4j.properties");
            logProperties.load(fis);
        }
        return logProperties;
    }

    public static Properties getLogPro() throws IOException {
        return  instance.geLogProperties();
    }

    /** 
     * 获取某个属性 
     */  
    public String getProperty(String key){  
        return props.getProperty(key);  
    }  
    /** 
     * 获取所有属性，返回一个map,不常用 
     * 可以试试props.putAll(t) 
     */  
    public Map getAllProperty(){  
        Map map=new HashMap();
        Enumeration enu = props.propertyNames();
        while (enu.hasMoreElements()) {  
            String key = (String) enu.nextElement();  
            String value = props.getProperty(key);  
            map.put(key, value);  
        }  
        return map;  
    }
}  